package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/
@RestController
@RequestMapping("/user")
public class UserController {

	@Reference
	private UserService userService;

	@RequestMapping("/getUsername")
	public Result getUsername() {

		//获取上下文对象中的user对象
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username = user.getUsername();


		if (user != null) {
			return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
		}
		return new Result(false, MessageConstant.GET_USERNAME_FAIL);
	}

	@RequestMapping("/findAllRole")
	public Result findAllRole() {

		try {
			List<Role> roleList = userService.findAllRole();
			return new Result(true,"",roleList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}

	}

	@RequestMapping("/add")
	public Result add(@RequestBody com.itheima.pojo.User user,Integer[] roleIds) {

		try {
			userService.add(user,roleIds);
			return new Result(true,"");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}

	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

		try {
			PageResult pageResult = userService.findPage(queryPageBean);
			return pageResult;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}

	}

	@RequestMapping("/findById")
	public Result findById(Integer id) {

		try {
			com.itheima.pojo.User user = userService.findById(id);
			//取出roleIds
			List<Integer> list = new ArrayList<>();
			Set<Role> roles = user.getRoles();
			for (Role role : roles) {
				list.add(role.getId());
			}


			Map<String,Object> map = new HashMap<>();
			map.put("user",user);
			map.put("roleIds",list);

			return new Result(true,"",map);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}

	}
	@RequestMapping("/edit")
	public Result edit(@RequestBody com.itheima.pojo.User user,Integer[] roleIds) {

		try {
			userService.update(user,roleIds);
			return new Result(true,"");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}

	@RequestMapping("/delete")
	public Result delete(Integer id) {

		try {
			userService.delete(id);
			return new Result(true,"");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}







}
